<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */

/**
 * 日志操作类
 */
class log_model extends model{
    /**
     * 添加管理员日志
     * @param string $content
     * @param string $opera:
     * @param string $opera_id
     */
	public function addAdminLog($content, $opera = '', $type = '', $opera_id=''){
		if($_SESSION['auid'] && $_SESSION['ausername'] && $content){
		    $data = array(
		        'uid'        =>  $_SESSION['auid'],
		        'username'   =>  $_SESSION['ausername'],
		        'content'    =>  $content,
		        'ctime'      =>  time(),
		        'ip'         =>  fun_ip_get(),
		        'opera'      =>  $opera,
		        'type'       =>  $type,
		        'opera_id'   =>  $opera_id,
		        'did'        =>  $this->config['did']
		    );
			
			$this->insert_into('admin_log',$data);
		}
	}
	/**
	 * 查询管理员日志
	 * @param array $whereData
	 * @param array $data
	 */
	public function getAdminLog($whereData = array(), $data = array()){
	    
	    $log  =  $this -> select_once('admin_log',$whereData);
	    
	    return $log;
	}
	/**
	 * 查询管理员日志
	 * @param array $whereData
	 * @param array $data
	 */
	public function getAdminLogList($whereData = array(), $data = array()){
		
		$data['field']	=	empty($data['field']) ? '*' : $data['field'];
	    
	    $List  			=  	$this -> select_all('admin_log',$whereData,$data['field']);
	    
	    return $List;
	}
	
	public function delAdminlog($whereData = array(), $data = array()) {
		
		$return['layertype']	=	0;
		
		if(!empty($whereData)){
		
			if(!empty($whereData['id']) && $whereData['id'][0] == 'in'){
				$return['layertype']	=	1;
			}
			if($data['norecycle'] == '1'){	//	数据库清理，不插入回收站

				$return['id']	=	$this -> delete_all('admin_log', $whereData, '', '', '1');
			}else{
				
				$return['id']	=	$this -> delete_all('admin_log', $whereData, '');
			}		
			$return['msg']		=	'后台日志';
			$return['errcode']	=	$return['id'] ? '9' :'8';
			$return['msg']		=	$return['id'] ? $return['msg'].'删除成功！' : $return['msg'].'删除失败！';
		}else{
			$return['msg']		=	'请选择您要删除的后台日志！';
			$return['errcode']	=	8;
		}
		return	$return; 
	}
    /**
     * 添加会员日志
     * @param string $uid
     * @param string $usertype
     * @param string $content
     * @param string $opera
	 1-职位，2-简历，3-下载简历（下载，删除），4-邀请面试（邀请，删除），5-收藏/关注（收藏/关注，删除/取消），6-申请/报名/委托，
	 
	 7-基本信息，8-修改密码，9-兼职，10-猎头，11-修改账号，12-账号解绑，13-账号认证，14-招聘会/专题，15-地图设置，16-图片（横幅，环境，作品），
	 
	 17-积分兑换，18-系统信息，19-问答，20-讲师，21-课程，22-新闻，23-举报，24-优惠券，25-悬赏推荐，26-浏览/屏蔽，27-子账号增删 ,28-搜索器
	 
	 29-项目任务,30-购买聊天,31注销账号申请
     * @param string $type:1-增加，2-修改，3-删除 , 4-刷新
	 
     */
	public function addMemberLog($uid,$usertype,$content,$opera='',$type='', $num = ''){
	    
	    if ($uid && $usertype && $content){
	        
	        $data  =  array(
	            'uid'       =>  $uid,
	            'usertype'  =>  $usertype,
	            'content'   =>  $content,
	            'opera'     =>  $opera,
	            'type'      =>  $type,
	            'ip'        =>  fun_ip_get(),
	            'ctime'     =>  time(),
	            'jobnum'    =>  $num
	        );
	        $member			=	$this->select_once('member',array('uid'=>$uid),'`did`');
			$data['did']	=	$member['did'];
			
	        $this -> insert_into('member_log',$data);
	    }
	}
  
    /**
     * @desc 获取user_log  单条
     * $whereData 	 查询条件
     * $data 		 自定义处理数组
     */
    public function getUserLog($whereData, $data = array()) {
        $data['field']	=	empty($data['field']) ? '*' : $data['field'];
        
        $List			=	$this -> select_once('user_log', $whereData, $data['field']);   
        
        return $List;
    }
	
	/**
     * @desc 获取user_log  列表
     *      $whereData  查询条件
     *      $data       自定义处理数组
     * @desc opera（访问滞留页面） : 10-基本信息， 11-基本信息（WAP）， 12-账号认证， 13-账号认证（WAP）， 21-新建简历， 22- 新建简历（WAP），
     *  31-发布职位， 32-修改职位，33-兼职发布， 34 - 兼职修改    
     *  35-发布职位（WAP）， 36-修改职位（WAP）， 37-兼职发布（WAP）， 38-兼职修改（WAP）
     *  41-充值积分 42-套餐会员 43-时间会员 44-增值服务
     *  45-充值积分（wap） 46-套餐会员（WAP）  47-时间会员（WAP） 48- 增值服务（WAP）
     */
    public function getUserLogList($whereData, $data = array()) {
        
        $data['field']	=	empty($data['field']) ? '*' : $data['field'];
       
        $List			=	$this -> select_all('user_log', $whereData, $data['field']);   
        
		if(!empty($List)){
			
			if($data['utype']=='admin'){
				foreach($List as $v){
					$uids[]			=	$v['uid'];
					if($v['orderid']!=''){
						$orderids[]	=	$v['orderid'];
					}
				}
				$where['uid']		=	array('in',pylode(",",$uids));
				
				$member				=	$this->select_all("member",$where,"`username`,`uid`,`usertype`");
				$company			=	$this->select_all("company",$where,"`uid`,`name`,`linkman`,`linktel`,`linkphone`");
				$resume				=	$this->select_all("resume",$where,"`uid`,`name`,`sex`,`telphone`");
				$orders				=	$this->select_all("company_order",array('order_id'=>array('in',pylode(",",$orderids))) ,"`order_id`,`order_type`,`order_state`");
				include (APP_PATH."/config/db.data.php");
				
				foreach($List as $k=>$v){
					foreach($member as $val){
						if($v['uid']==$val['uid']){
							$List[$k]['username']	=	$val['username'];
							$List[$k]['usertype']	=	$val['usertype'];
						}
					}

					foreach($company as $val){
						if($v['uid']==$val['uid']){
							$List[$k]['comname']	=	$val['name'];
							$List[$k]['name']		=	$val['linkman'];
							if($val['linktel']!=""){
								$List[$k]['mobile']	=	$val['linktel'];
							}else{
								$List[$k]['mobile']	=	$val['linkphone'];
							}
						}
					}

					foreach($resume as $val){
						if($v['uid']==$val['uid']){
							$List[$k]['name']		=	$val['name'];
							$List[$k]['sex_n']		=	$arr_data['sex'][$val['sex']];
							$List[$k]['mobile']		=	$val['telphone'];
						}
					}

					//滞留时间转换为"X分X秒"格式
					if($v['second']>60){
						
						$h = floor(($v['second'] % (3600*24)) / 3600);
						$m = floor((($v['second'] % (3600*24)) % 3600) / 60);
						$s = floor((($v['second'] % (3600*24)) % 3600 % 60));

						if($h!='0'){
							$List[$k]['seconds']	=	$h.'时'.$m.'分';
						}else if($m!='0'){
							if($m>5){
								$List[$k]['seconds']=	'5分钟以上';
							}else{
								$List[$k]['seconds']=	$m.'分'.$s.'秒';
							}
						} 
					}else{
						$List[$k]['seconds']		=	$v['second'].'秒';
					}
				
					foreach($orders as $val){
						if($v['orderid']==$val['order_id']){
							$List[$k]['order_type_n']	=	$arr_data['pay'][$val['order_type']];
							$List[$k]['order_state']	=	$val['order_state'];
						}
					}
				}
				
			}
			
			if ($data['utype'] == 'crm') {
			    foreach ($List as $k => $v) {
			        $List[$k]['time']    =   date('Y-m-d h:i:sa', $v['timein']);
			        $List[$k]['urlPage']     =   $this->getBrowserUrl($v['opera']);
			        
			        if($v['second']>60){
			            
			            $h = floor(($v['second'] % (3600*24)) / 3600);
			            $m = floor((($v['second'] % (3600*24)) % 3600) / 60);
			            $s = floor((($v['second'] % (3600*24)) % 3600 % 60));
			            
			            if($h!='0'){
			                $List[$k]['seconds']	=	$h.'时'.$m.'分';
			            }else if($m!='0'){
			                if($m>5){
			                    $List[$k]['seconds']=	'5分钟以上';
			                }else{
			                    $List[$k]['seconds']=	$m.'分'.$s.'秒';
			                }
			            }
			        }else{
			            $List[$k]['seconds']		=	$v['second'].'秒';
			        }
			    }
			}
			
		}
		
        return $List;
    }
    
    /**
     * @desc  获取访问页面
     * @param string $opera
     */
    private function getBrowserUrl($opera = ''){
        
        $urlArr =   array(
            '10'    =>  '基本信息页面',
            '11'    =>  '基本信息WAP',
            '12'    =>  '账号认证',
            '13'    =>  '账号认证WAP',
            '21'    =>  '新建简历',
            '22'    =>  '新建简历WAP',
            '31'    =>  '发布职位',
            '32'    =>  '修改职位',
            '33'    =>  '兼职发布',
            '34'    =>  '兼职修改',
            '35'    =>  '发布职位WAP',
            '36'    =>  '修改职位WAP',
            '37'    =>  '兼职发布WAP',
            '38'    =>  '兼职修改WAP',
            '41'    =>  '充值积分',
            '42'    =>  '套餐会员',
            '43'    =>  '时间会员',
            '44'    =>  '增值服务',
            '45'    =>  '充值积分WAP',
            '46'    =>  '套餐会员WAP',
            '47'    =>  '时间会员WAP',
            '48'    =>  '增值服务WAP'
        );
        
        $urlPage    =   $urlArr[$opera];
        
        return $urlPage;
    }
    
    /**
     * 添加营销-反馈提醒
     * @param array $data
     */
	public function addUserLog($data = array()){
	    
	    if(!empty($data)){
		    
		    $sdata  =  array(
		        'uid'       =>  intval($data['uid']),
		        'usertype'  =>  intval($data['usertype']),
		        'url'       =>  $data['url'],
		        'refer'     =>  $data['refer'],
		        'opera'     =>  $data['opera'],
		        'orderid'   =>  $data['orderid'],
		        'timein'    =>  $data['timeIn'],
		        'timeout'   =>  $data['timeIn'] + $data['second'],
		        'second'    =>  $data['second']
		    );
		    
		    $nid  =  $this -> insert_into('user_log',$sdata);
		    
			return $nid;
		}
	}
    /**
     * 修改营销-反馈提醒
     * @param array $data
     */
	public function upUserLog($data = array(),$where=array()){
	    
		if($data['id']){
		    
		    $log    =  $this -> select_once('user_log',array('id'=>intval($data['id'])),'`timein`');
		    
		    $sdata  =  array(
		        'timeout'  =>  $log['timein'] + $data['second'],
		        'second'   =>  $data['second']
		    );
		    
		    $nid    =  $this -> update_once('user_log',$sdata,array('id'=>$data['id']));
		}elseif($where){
			$nid    =  $this -> update_once('user_log',$data,$where);
		}
	}
  
    /**
     * 统计营销
     * @param array $data
     */
	public function getUserLogNum($whereData=array()){
    
        return $this -> select_num('user_log',$whereData);
	}
	/**
	 * 查询会员日志数目
	 */
	function getMemberLogNum($Where=array()){
	    return $this->select_num('member_log', $Where);
	}
	/**
     * 获取member_log 列表
	 * $whereData 	 查询条件
     * $data 		 自定义处理数组
     */
    public function getMemlogList($whereData, $data = array()) {
        $data['field']  =	empty($data['field']) ? '*' : $data['field'];
        $List  			=   $this -> select_all('member_log', $whereData, $data['field']);   
        
        if ($data['utype'] == 'admin'){
            
            $List  =  $this->getDataList($List);
        }
        return $List;    
	}

	/**
     *@desc 删除member_log日志
	 * $delId 日志id
     */
    public function delMemlog($whereData = array(), $data = array()) {

		$return['layertype']	=	0;
		
		if(!empty($whereData)){
		
			if(!empty($whereData['id']) && $whereData['id'][0] == 'in'){
				$return['layertype']	=	1;
			}
			
			if($data['norecycle'] == '1'){	//	数据库清理，不插入回收站

				$return['id']		=	$this -> delete_all('member_log', $whereData, '', '', '1');
			}else{
				
				$return['id']		=	$this -> delete_all('member_log', $whereData, '');
			}

			$return['msg']		=	'会员日志';
			$return['errcode']	=	$return['id'] ? '9' :'8';
			$return['msg']		=	$return['id'] ? $return['msg'].'删除成功！' : $return['msg'].'删除失败！';
		}else{
			$return['msg']		=	'请选择您要删除的会员日志！';
			$return['errcode']	=	8;
		}
		return	$return; 
	}
	
	/**
     * @desc 获取login_log 列表
     */
    public function getLoginlogList($whereData, $data = array()) {
        
        $data['field']  =	empty($data['field']) ? '*' : $data['field'];
        $List  			=   $this -> select_all('login_log', $whereData, $data['field']);
        
        if ($data['utype'] == 'admin'){
            
            $List  =  $this -> getDataList($List);
        }
        
        return $List;    
	}
	/**
     * @desc 添加login_log
     */
    public function addLoginlog($addData, $data = array()) {
        
        $return			=	0;
        
        if (!empty($addData)){
            $addData['remoteport'] = !empty($addData['remoteport']) ? $addData['remoteport'] :$_SERVER['REMOTE_PORT'];
            $return 	=   $this -> insert_into('login_log', $addData);
        }
        
        return $return;    
	}
	public function getLoginLog($where = array())
	{
	    $return  =  $this -> select_once('login_log', $where);
	    return  $return;
	}
	
	/**
     * @desc 删除login_log日志
	 * $delId 日志id
     */
    public function delLoginlog($delId,$where) {
		$return['layertype']	=	0;
		
		if(!empty($delId)){
			if(is_array($delId)){
				$delId					=	pylode(',', $delId);

				$return['layertype']	=	1;
			}
			
			$return['id']				=	$this -> delete_all("login_log", array('id' => array('in',$delId)),"");
			
			$return['msg']				=	'会员登录日志(ID:'.$delId.')';
			$return['errcode']			=	$return['id'] ? '9' :'8';
			$return['msg']				=	$return['id'] ? $return['msg'].'删除成功！' : $return['msg'].'删除失败！';
		}elseif($where){
			$return['id']				=	$this -> delete_all("login_log", $where,"");
			
			$return['msg']				=	'会员登录日志';
			$return['errcode']			=	$return['id'] ? '9' :'8';
			$return['msg']				=	$return['id'] ? $return['msg'].'删除成功！' : $return['msg'].'删除失败！';
		}else{
			$return['msg']				=	'请选择您要删除的会员登录日志！';
			$return['errcode']			=	8;
		}
		return	$return; 
	}
	//会员日志后台列表数据处理
	private function getDataList($List){
	    
	    foreach($List as $v){
	        
	        $uids[]   =   $v['uid'];
	    }
	    $member    =  $this->select_all('member',array('uid'=>array('in',pylode(',', $uids))),'`uid`,`username`');
	    $resume    =  $this->select_all('resume',array('uid'=>array('in',pylode(',', $uids))),'`uid`,`name`');
		$company   =  $this->select_all('company',array('uid'=>array('in',pylode(',', $uids))),'`uid`,`name`');
		$gqinfo    =  $this->select_all('gq_info',array('uid'=>array('in',pylode(',', $uids))),'`uid`,`name`');
	    
	    foreach($List as $k=>$v){
	        
	        foreach($member as $val){
	            
	            if($val['uid']==$v['uid']){
	            
	                $List[$k]['username']  =  $val['username'];
	            }
	        }
	        foreach($resume as $val){
	            
	            if($v['uid']==$val['uid']){
	                
	                $List[$k]['rname']  =  $val['name'];
	            }
	        }
	        foreach($company as $val){
	            
	            if($v['uid']==$val['uid']){
	                
	                $List[$k]['comname']  =  $val['name'];
	            }
			}
			foreach($gqinfo as $val){
	            
	            if($v['uid']==$val['uid']){
	                
	                $List[$k]['gqname']  =  $val['name'];
	            }
	        }
	    }
	    return $List;
	}
	/**
    * @desc    删除用户行为数据
    */
	public function delEventlog($delId){
		if(empty($delId)){
			return	array('errcode'=>8,'msg'=>'请选择要删除的数据！');
		}else{
			if(is_array($delId)){
				$delId	=	pylode(',',$delId);
				$return['layertype']	=	1;
			}else{	
				$return['layertype']	=	0;
			}
			$nid	=	$this->delete_all('user_log',array('id'=>array('in',$delId)),'');
			if($nid){
				$return['msg']		=	'记录(ID:'.$delId.')';	
				$return['errcode']	=	$nid ? '9' :'8';			
				$return['msg']		=	$nid ? $return['msg'].'删除成功！' : $return['msg'].'删除失败！';
			}
		}
		return	$return;
	}
}

?>